package com.danikula.videocache

import android.content.Context
import com.danikula.videocache.file.CacheDirUtils
import java.io.Serializable

private const val DEFAULT_MAX_SIZE = 1024 * 1024 * 1024L

class ProxyCacheManager : Serializable {

    private object ProxyCacheManagerHolder {
        val mInstance = ProxyCacheManager()
    }

    companion object {
        @JvmStatic
        fun getInstance(): ProxyCacheManager {
            return ProxyCacheManagerHolder.mInstance
        }

        private var proxy: HttpProxyCacheServer? = null

        fun getProxy(context: Context): HttpProxyCacheServer {
            return proxy ?: getInstance().newProxy(context).also { proxy = it }
        }
    }

    private fun readResolve(): Any {
        return ProxyCacheManagerHolder.mInstance
    }

    private fun newProxy(context: Context): HttpProxyCacheServer {
        return HttpProxyCacheServer.Builder(context)
            .cacheDirectory(CacheDirUtils.getVideoCacheDir(context))
            .maxCacheSize(DEFAULT_MAX_SIZE) // 1G
            .build()
    }
}